package com.zyc.webtest.shiro;

import com.zyc.webtest.domain.SysUserEntity;
import com.zyc.webtest.service.SysMenuService;
import com.zyc.webtest.service.SysRoleService;
import com.zyc.webtest.service.SysUserService;
import com.zyc.webtest.utils.SpringContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
 * Created by zyc on 2017/4/24.
 */
public class MyShiroRealm  extends AuthorizingRealm {
    private static final Logger logger = LoggerFactory.getLogger(MyShiroRealm.class);

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("##################执行Shiro权限认证##################");
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        Set<String> set = new HashSet<String>();
        SysRoleService sysRoleService = SpringContextUtils.getBean("sysRoleService",SysRoleService.class);
        SysMenuService sysMenuService = SpringContextUtils.getBean("sysMenuService",SysMenuService.class);
        sysRoleService.findAll().ifPresent(a->a.forEach(b->set.add(b.getRoleName())));

//        set.add("user");
//        set.add("admin");
        info.setRoles(set);
        set.stream().forEach(a->{
            sysMenuService.findRoleMenu(a).
                ifPresent(c->c.stream().forEach(d-> {
                    if(StringUtils.isNotEmpty(d.getPerms()))
                        info.addStringPermission(d.getPerms());
                }));
        });
//        info.addStringPermission("user:list");
//        info.addStringPermission("admin");
//        info.addStringPermission("user");
        return info;

    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
        SysUserService sysUserService = SpringContextUtils.getBean("sysUserService",SysUserService.class);
//        token.get
        logger.info("验证当前Subject时获取到token为：" +token.getUsername());
        Optional<SysUserEntity> user = sysUserService.findUserByUsername(token.getUsername());
        SimpleAuthenticationInfo simpleAuthenticationInfo = null;
//        if(user.isPresent()){
//            SysUserEntity a =user.get();
//            simpleAuthenticationInfo = new SimpleAuthenticationInfo(a.getUserId(), a.getPassword(), a.getUsername());
//        }
        simpleAuthenticationInfo = user.filter(a->token.getPassword().toString().equals(a.getPassword()))
                .map(b->new SimpleAuthenticationInfo(b.getUserId(), b.getPassword(), b.getUsername()))
                .orElse(null);
        return simpleAuthenticationInfo;
    }
}
